package com.aopeng.controller;

import com.aopeng.entity.AopengUser;
import com.aopeng.mapper.AopengUserMapper;
import com.aopeng.service.MailService;
import com.aopeng.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author 谢辉
 * @description 用户控制器
 * @data 2020/7/18
 */
@RequestMapping("/user")
@Controller
public class AopengUserController {

    @Autowired
    private AopengUserMapper aopengUserMapper;

    @Autowired
    private MailService mailService;

    private String EMAIL_PASSWORD_URL= "http://localhost:8080/user/emailform";


    /**
     * 跳转到密码修改页
     * @author   谢辉
     * @date      2020/7/17 13:57
     */
    @RequestMapping(value = "/altpwd",method = RequestMethod.GET)
    public String updateLoginPasswordPage(){
        return "admin/user-altpassword";
    }

    /**
     * 修改后提交的表单处理方法
     * @author   谢辉
     * @date      2020/7/17 13:57
     */
    @RequestMapping(value = "/pwdform",method = RequestMethod.POST)
    public String updateLoginPassword(@RequestParam("oldLoginPassword")String oldLoginPassword,
                                      @RequestParam("newLoginPassword1")String newLoginPassword1,
                                      @RequestParam("newLoginPassword2")String newLoginPassword2,
                                      HttpSession session, RedirectAttributes attributes){
        AopengUser user = (AopengUser) session.getAttribute("user");
        AopengUser aopenguser = aopengUserMapper.selectById(user.getId());

        //如果输入的旧密码和查数据库一致的话则为true
        if (MD5Utils.code(oldLoginPassword).equals(aopenguser.getLoginPassword()) || oldLoginPassword.equals(aopenguser.getLoginPassword())){
            if (MD5Utils.code(newLoginPassword1).equals(MD5Utils.code(newLoginPassword2))){
                AopengUser auser = new AopengUser();
                auser.setId(user.getId());
                auser.setLoginPassword(MD5Utils.code(newLoginPassword2));
                aopengUserMapper.updateById(auser);
                attributes.addFlashAttribute("message", "修改成功");
                return "redirect:/authority/altpwd";
            }else {
                attributes.addFlashAttribute("message", "两次输入的密码不一致");
                return "redirect:/authority/altpwd";
            }

        }else{
            attributes.addFlashAttribute("message", "旧密码错误");
            return "redirect:/authority/altpwd";
        }
    }

    /**
    * 忘记密码
    * @author   谢辉
    * @date      2020/7/18 4:05
    */
    @RequestMapping(value = "/forget",method = RequestMethod.POST)
    public String foGetLoginPasswordPage(@RequestParam("email")String email,
                                         RedirectAttributes attributes,
                                         HttpSession session){
        //1.发送邮件(包含密码)
        //2.根据邮箱号查询是否有该记录
        AopengUser userByEmail = aopengUserMapper.findUserByEmail(email);
        if (userByEmail!=null){
            session.setAttribute("userByEmail",userByEmail);
            String subject = "来自奥鹏作业辅助的密码更改";
            //上面的激活码发送到用户注册邮箱
            String context = "请在此链接中更改您的新密码 http://localhost:8080/user/emailform!";
            //发送激活邮件
            mailService.sendMimeMail(email.trim(), subject, context);

            attributes.addFlashAttribute("message", "邮件已发送！");
            return "redirect:/admin";

        }else {
            attributes.addFlashAttribute("message", "您的预留邮箱错误！");
            return "redirect:/admin";
        }
    }

    @RequestMapping(value = "/emailform",method = RequestMethod.GET)
    public String emailFormPage(){

        return "admin/email-updatepwd";
    }

    @RequestMapping(value = "/updatepwd",method = RequestMethod.POST)
    public String updatepwd(@RequestParam("loginPassword")String loginPassword,
                            @RequestParam("loginPassword1")String loginPassword1,
                            HttpSession session,RedirectAttributes attributes){
        AopengUser userByEmail = (AopengUser) session.getAttribute("userByEmail");
        if (loginPassword.equals(loginPassword1)){
            AopengUser aopengUser = new AopengUser();
            aopengUser.setId(userByEmail.getId());
            aopengUser.setLoginPassword(MD5Utils.code(loginPassword1));
            aopengUserMapper.updateById(aopengUser);

            attributes.addFlashAttribute("message", "密码修改成功！");
            return "redirect:/admin";
        }
        attributes.addFlashAttribute("message", "两次输入的密码不一致！");
        return "redirect:/admin";
    }
}
